<?php
/*
 * 推荐位管理model
 * @auth suosuo
 */
namespace Common\Model;
use Think\Model;

class PositionModel extends Model{
	private $_db = '';
	
	public function __construct (){
		$this->_db = M('position');
		
	}
	
	public function getPosition ($data,$page,$pageSize=10){
		$conditions = $data;
		
		$offset = ($page - 1) * $pageSize;
		$list = $this->_db->where($conditions)
		->order('id desc')
		->limit($offset,$pageSize)
		->select();
		return $list;
	}
	
	public function getNormalPositions () {//获取正常推荐位列表
		$conditions = array ('status'=>1);
		$list = $this->_db->where($conditions)->order('id')->select();
		return $list;
	}
	public function getPositionCount ($data = array()){
		$conditions = $data;
		if(isset($data['name']) && $data['name']){
			$conditions['name'] = array ('like','%'.$data['name'].'%');
		}
		if(isset($data['id']) && $data['id']){
			$conditions['id'] = intval($data['id']);
		}
		return $this->_db->where($conditions)->count();
	}
	
	public function updataStatusById ($id,$status) {
		if(!is_numeric($id) || !$id){
			throw_exception("ID不合法");
		}
		if(!is_numeric($status) || !$status){
			throw_exception("状态不合法");
		}
		$data['status']=$status;
		return $this->_db->where('id='.$id)->save($data);
	}
	
	public function getPositionById ($id) {
		if(!is_numeric($id) || !$id){
			throw_exception("ID不合法");
		}
		$data = $this ->_db->where('id='.$id)->find();
		return $data;
	}
	public function updataPositionById ($id,$data) {
		if( !$id || !is_numeric($id) ){
			throw_exception('ID不合法');
		}
		if( !$data || !is_array($data) ){
			throw_exception('更新的数据不合法');
		}
		$data['update_time']=time();
		return $this->_db->where('id='.$id)->save($data);
	}
	
	public function insert ($data = array()) {
		if(!$data || !is_array($data)){
			return 0;
		}
		$data['create_time']=time();
		return $this->_db->add($data);
	}
	public function getPositionNames(){
		$data = array(
			'status' => array('neq',-1),
		);
		$res = $this->_db->where($data)
		->order('id desc')
		->select();
		return $res;
	}
}
